﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using StuMngVan.Entities;
using DX.CRC.Common.Entities;

namespace StuMngVan.Dals
{
    class Dal4Student : DalBase
    {
        public static List<Student> SearchAllStudents(string txtname)
        {
            QueryObject qo = CreateQO();

            qo.CmdText = @"SELECT [ID]
                    ,[Name]
                    ,[Address]
                    ,[Age]
                    ,[IsActive]
                    ,[CreateTime]
                    ,[Remark]
                FROM [TbStudent] WHERE IsActive=1 and Name like '%'+@txtName+'%'";

            qo.AddParam("@txtName", txtname);

            return qo.xExecuteList<Student>();
        }

        ///【闻祖东 2013-3-28-192033】我删除这个方法，是希望告诉你，能用对象替换的时候，就用对象替换。
        ///你所传递的四个参数，那不过是四个冷冰冰的字符串或者数字而已，他们彼此毫无关系。但是如果你
        ///本身传递的是一个类，那么这个才是真正的对象。
        ///这个才算是面向对象编程。
//        public static void InsertStudent(string name, string address, int age, string remark)
//        {
//            QueryObject qo = CreateQO();

//            qo.CmdText = @"INSERT INTO [dbo].[TbStudent]
//                   ([Name]
//                   ,[Address]
//                   ,[Age]
//                   ,[IsActive]
//                   ,[CreateTime]
//                   ,[Remark])
//             VALUES
//                   (@Name
//                   ,@Address
//                   ,@Age
//                   ,1
//                   ,getutcdate()
//                   ,@Remark)";

//            qo.AddParam("@Name", name);
//            qo.AddParam("@Address", address);
//            qo.AddParam("@Age", age);
//            qo.AddParam("@Remark", remark);

//            qo.xExecuteNonQuery();
//        }

        public static void InsertStudent(Student stu)
        {
            QueryObject qo = CreateQO();

            qo.CmdText = @"INSERT INTO [dbo].[TbStudent]
                   ([Name]
                   ,[Address]
                   ,[Age]
                   ,[IsActive]
                   ,[CreateTime]
                   ,[Remark])
             VALUES
                   (@Name
                   ,@Address
                   ,@Age
                   ,1
                   ,getutcdate()
                   ,@Remark)";

            qo.AddParam("@Name", stu.Name);
            qo.AddParam("@Address", stu.Address);
            qo.AddParam("@Age", stu.Age);
            qo.AddParam("@Remark", stu.Remark);

            qo.xExecuteNonQuery();
        }

        public static void DeleteStudent(Student student)
        {
            QueryObject qo = CreateQO();

            qo.CmdText = @"update TbStudent 
                  set IsActive = 0 
                  WHERE ID=@StudentID";

            qo.AddParam("@StudentID", student.ID);

            qo.xExecuteNonQuery();
        }

        public static void UpdateAge()
        {
            QueryObject qo = CreateQO();

            qo.CmdText = @"declare @Age int
                           set @Age=1
                           update TbStudent set Age=Age+@Age where IsActive=1";

            qo.xExecuteNonQuery();

        }
    }
}
